﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BagPanelExt : PanelBase
{
    private Hashtable gridsHash;
    private Transform ItemTips;

    public override void RegisterListeners()
    {
        base.RegisterListeners();
        gridsHash = new Hashtable();
        ItemTips = transform.Find("ItemTips");
        //取消射线检测
        ItemTips.GetComponent<Image>().raycastTarget = false;
        
        GridInit();
    }

    public override void Show()
    {
        base.Show();
        Mgr.bagMgr.bagEvent.AddListener(BagEvent.BAGITEMADD, OnItemAddHandler);
        Mgr.bagMgr.bagEvent.AddListener(BagEvent.BAGITEMCHANGE, OnItemChangeHandler);
        Mgr.bagMgr.bagEvent.AddListener(BagEvent.BAGITEMDEL, OnItemDelHandler);
        Mgr.bagMgr.bagEvent.AddListener(BagEvent.BagItemMove, OnItemMoveHandler);

        UpdateItemList();
        ItemTips.gameObject.SetActive(false);
    }

    public override void Hide()
    {
        base.Hide();
        Mgr.bagMgr.bagEvent.RemoveListener(BagEvent.BAGITEMADD, OnItemAddHandler);
        Mgr.bagMgr.bagEvent.RemoveListener(BagEvent.BAGITEMCHANGE, OnItemChangeHandler);
        Mgr.bagMgr.bagEvent.RemoveListener(BagEvent.BAGITEMDEL, OnItemDelHandler);
        Mgr.bagMgr.bagEvent.RemoveListener(BagEvent.BagItemMove, OnItemMoveHandler);
    }

    public override void PanelUpdate()
    {
        base.PanelUpdate();
    }

    private void GridInit()
    {//初始化格子
        Transform obj = transform.Find("Grids");
        foreach(Transform child in obj.transform)
        {
            string name = child.name;
            name = name.Remove(0, 4);
            Transform GridImg = child.Find("Item");
            gridsHash.Add(int.Parse(name), GridImg);
            Item item = GridImg.gameObject.AddComponent<Item>();
            item.Init(int.Parse(name));

            Image img = child.Find("Image").gameObject.GetComponent<Image>();
            img.raycastTarget = false;
        }
        Item.OnEnter += OnMousetInItem;
        Item.OnExit += OnMousetOutItem;
    }


    private void UpdateItemList()
    {//打开面板的时候更新道具
        foreach(Transform grid in gridsHash.Values)
        {
            SetGridImg(grid);
        }


        var items = Mgr.bagMgr.itemInfos;
        foreach(ItemInfo item in items)
        {
            Transform grid = gridsHash[item.GridId] as Transform;
            SetGridImg(grid, item);
        }
    }

    

    private void OnItemAddHandler(object obj)
    {
        ItemInfo item = obj as ItemInfo;
        SetGridImg(gridsHash[item.GridId] as Transform, item);
    }

    private void OnItemChangeHandler(object obj)
    {
        ItemInfo item = obj as ItemInfo;

    }

    private void OnItemDelHandler(object obj)
    {
        ItemInfo item = obj as ItemInfo;
        SetGridImg(gridsHash[item.GridId] as Transform, null);
        OnMousetOutItem();
    }

    private void OnItemMoveHandler(object obj)
    {
        int index = (int)obj;
        ItemInfo item = Mgr.bagMgr.GetItemByGridIdx(index);
        if (item != null)
        {
            SetGridImg(gridsHash[index] as Transform, item);
        }
        else
        {
            SetGridImg(gridsHash[index] as Transform, null);
        }
    }

    //修改格子图标
    private void SetGridImg( Transform grid, ItemInfo itemInfo = null)
    {
        Item item = grid.GetComponent<Item>();
        item.SetItemInfo(itemInfo);
    }

    private void OnMousetInItem(Transform transform, string str)
    {//显示道具tips
        ItemTips.gameObject.SetActive(true);
        Vector3 point = transform.position;
        point.x += (ItemTips as RectTransform).rect.width / 3;
        point.y -= (ItemTips as RectTransform).rect.height / 3;
        ItemTips.position = point;
        ItemTips.GetComponentInChildren<Text>().text = str;
    }

    private void OnMousetOutItem()
    {
        ItemTips.gameObject.SetActive(false);
    }

}
